<?php 
class Admin extends CI_Controller{

	
function index(){
	
	$this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|max_length[50]|xss_clean');
	$this->form_validation->set_rules('password', 'Password', 'required|trim|alpha_numeric|max_length[200]|xss_clean');
	if($this->form_validation->run() == FALSE){		
		$this->load->view('backend/login');
	}
	else{
		extract($_POST);
		
		$user_id = $this->madmin->login($username, $password);
		if (! $user_id){
			//login failed error
			$this->session->set_flashdata('login_error', TRUE);
			redirect('admin');
			
			
		}
		else{
			// login in		

		$this->session->set_userdata(array(
										'logged_in'=>TRUE,
										 'user_id'=>$user_id
										 ));
			redirect('admin/manager');
		}
	}	
}
function logout(){	
	$this->session->sess_destroy();
	redirect('admin');
}


function manager($action=Null){

	if($this->session->userdata('logged_in'))
	{	$param              = $this->uri->segment(4); 	    
		$data['content']    = $action;
		$data['backend']	= base_url().'index.php/admin/manager/';		
		$data['backendimg']	= base_url().'public/backend/images/';
		switch ($action){

		case ''             	   :$data['content'] = 'index';			        break;

		case 'edit_product'		   :$data['product'] = $this->mhome->admin_get_product($param);break;		

		case 'func_remove_product' :$this->mhome->remove_product($param);									
									$data['content'] = 'showproducts';			break;

		case 'func_restore_product':$this->mhome->restore_product($param);									
									$data['content'] = 'remove_product';			break;		
				
	    case 'func_delete_product' :$this->mhome->delete_product($param);	
	    						    $data['message'] ='ban da xoa thanh cong san pham';	    						    
	    						    $data['content'] = 'remove_product';          break;

	    case 'func_save_add_product':if($this->mhome->save_add_product()){
										$data['message'] = 'Product was added successfully ';
									}
									else{
										$data['message'] = 'Product was not added. Please check and try again';
									}
									
									$data['content'] = 'showproducts';            break;

		case 'func_save_edited_product':if($this->mhome->save_edited_product()){
										$data['message'] = 'Product was saved successfully';				
									}
									else{
										$data['message'] = 'Product was not saved. Please make sure the fields
										 marked required are present and the image doesnt exceed the limits';
									}
									
									$data['content'] = 'showproducts';            break;	
	    
	    case 'func_add_a_category'  :if($this->mhome->add_a_category()){
						    			$data['message'] = 'category was added successfully';
						    		}
						    		else{
						    			$data['message'] = 'Category was not added. Please check if category name exists and
						    			it is below 100 characters';
						    		}						    		
									$data['content'] = 'showcategories';		    break;	

		case 'func_edit_category'	:$data['category'] = $this->mhome->get_a_category($param); 								   
								    $data['content'] = 'showcategories';	        break;	

		case 'func_save_edited_cat'	: $this->mhome->save_edited_cat();								   
									$data['content'] = 'showcategories';		    break;	

		case 'func_delete_category'	: if($this->mhome->delete_category($param))									{
										$data['message'] = "Danh mục đã được xóa";
									}
									else{
										$data['message'] = 'Không thể xóa danh mục này vì chứa sản phẩm';
									}									
									$data['content'] = 'showcategories';		    break;

		case 'func_remove_article' : $this->mhome->remove_article($param);
								     $data['content'] = 'article';				break;									
		
		case 'func_restore_article': $this->mhome->restore_article($param);
									 $data['content'] = 'removed_article'; 				break;	

		case 'func_save_add_article':if($this->mhome->save_add_article()){
										$data['message'] = 'Article was added successfully ';
									}
									else{
										$data['message'] = 'Article was not added. Please check and try again';
									}
									
									$data['content'] = 'article';            break;

		case 'func_save_edit_article':if($this->mhome->save_edit_article()){
										$data['message'] = 'Article was saved successfully ';
									}
									else{
										$data['message'] = 'Article was not saved. Please check and try again';
									}
									
									$data['content'] = 'article';            break;

		case 'func_delete_article' : $this->mhome->delete_article($param);
									 $data['content'] = 'removed_article';		  break;

			} 
		switch ($data['content']){

		case 'showproducts'	   : $this->load->library('pagination');
                         $config['base_url']         = base_url().'index.php/admin/manager/showproducts/';            
                         $config['total_rows']       = $this->mhome->get_total_rows();
                         //$config['use_page_numbers'] = TRUE;//hiển thị số trang đúng
                         $config['per_page']         = 4;  
                         $config['uri_segment']      = 4;                              
                         $this->pagination->initialize($config);
                         $data['products'] = $this->mhome->admin_get_products($config['per_page'], $param);                         
                          
                         break; 

		case 'article'        : $this->load->library('pagination');
                         $config['base_url']         = base_url().'index.php/admin/manager/article/';            
                         $config['total_rows']       = $this->mhome->get_total_rows_news();
                         //$config['use_page_numbers'] = TRUE;//hiển thị số trang đúng
                         $config['per_page']         = 5;  
                         $config['uri_segment']      = 4;                              
                         $this->pagination->initialize($config);                                              
                         $data['news'] = $this->mhome->get_news($config['per_page'], $param);
                         break;   

        case 'showcategories' : $data['categories'] = $this->mhome->get_categories(); break;

        case 'remove_product' : $data['products_removed'] = $this->mhome->admin_get_remove_products(); break;  
        case 'removed_article': $data['removed_article'] = $this->mhome->get_removed_news(); break;	    
        case 'edit_article'	  : $data['article'] = $this->mhome->get_article($param);
		}	    	    
	    	    	
		$this->load->view('backend/manager', $data);

	}
	else{
		redirect('admin');
	}	

	}

}
